//
//  EditBatteryTableViewCell.swift
//  ezyform
//
//  Created by Moennig Q on 15/9/2022.
//  Copyright © 2022 Client.Solution Pty Ltd. All rights reserved.
//
import UIKit
import RxSwift
import RxGesture
import RxCocoa
class EditBatteryTableViewCell: TableViewCell {
    var selectModelClosure: (()->Void)?
    var deleteRowClosure: (()->Void)?
    var partOfAggreSwitchClosure: ((Bool)->Void)?
    var changedSwitchClosure: ((Bool)->Void)?
    let backView = UIView()
    lazy var brandTextfield: DIYTextField = {
        let view = DIYTextField()
        view.clearButtonMode = .never
        view.disabled(textHexStr: "#292D3266", backHexStr: "#E3E3E3FF")
        return view
    }()
    lazy var seriesTextfield: DIYTextField = {
        let view = DIYTextField()
        view.clearButtonMode = .never
        view.disabled(textHexStr: "#292D3266", backHexStr: "#E3E3E3FF")
        return view
    }()
    lazy var modelTextfield: TextFieldWithRequiredView = {
        let view = TextFieldWithRequiredView()
        view.textField.isEnabled = false
        view.textField.clearButtonMode = .never
        view.textField.showRightImageView("installation_list_search_down_arrow")
        return view
     }()
    lazy var countTextfield: TextFieldWithRequiredView = {
        return TextFieldWithRequiredView(.positiveNumber3)
     }()
    
   lazy var batteryRatingTextfield: DIYTextField = {
       let view = DIYTextField()
       view.clearButtonMode = .never
       view.disabled(textHexStr: "#292D3266", backHexStr: "#E3E3E3FF")
       return view
    }()
    var partOfAggreSwitchBtn: SwitchButton!
    var changedSwitchBtn: SwitchButton!
   lazy var deleteBtn: UIButton = {
       let btn = UIButton()
       btn.setImage(UIImage(named: "PVDDetail_systeminfo_del"), for: UIControl.State.normal)
       return btn
    }()
   
    //
    override func setUpviews(){
        //backView
        backView.drawBorder(backHexStr: "#F0F6F3FF", borderHexStr: "#005D2D66")
        self.contentView.addSubview(backView)
        backView.snp.makeConstraints { make in
            make.top.left.right.equalToSuperview()
            make.height.greaterThanOrEqualTo(0)
            make.bottom.equalToSuperview().offset(-Constant.TopBottomMargin)
        }
        //brandTitle
        let brandTitle = ContentLabel()
        brandTitle.text = R.string.localizable.manufacturer()
        backView.addSubview(brandTitle)
        brandTitle.snp.makeConstraints { make in
            make.top.equalTo(Constant.TopBottomMargin)
            make.left.equalTo(Constant.LeftRightMargin)
            make.width.height.greaterThanOrEqualTo(0)
        }
        //delete btn
        backView.addSubview(deleteBtn)
        deleteBtn.snp.makeConstraints { make in
            make.top.right.equalToSuperview()
            make.width.height.equalTo(40)
        }
        //brandTextfield
        backView.addSubview(brandTextfield)
        brandTextfield.snp.makeConstraints { make in
            make.top.equalTo(brandTitle.snp.bottom).offset(Constant.TitleTextMargin)
            make.left.equalTo(brandTitle)
            make.right.equalTo(-Constant.LeftRightMargin)
            make.height.equalTo(DIYTextField.height)
        }
        //seriesTitle
        let seriesTitle = ContentLabel()
        seriesTitle.text = R.string.localizable.series()
        backView.addSubview(seriesTitle)
        seriesTitle.snp.makeConstraints { make in
            make.top.equalTo(brandTextfield.snp.bottom).offset(Constant.TopBottomMargin)
            make.left.equalTo(brandTitle)
            make.width.height.greaterThanOrEqualTo(0)
        }
        //seriesTextfield
        backView.addSubview(seriesTextfield)
        seriesTextfield.snp.makeConstraints { make in
            make.top.equalTo(seriesTitle.snp.bottom).offset(Constant.TitleTextMargin)
            make.left.height.right.equalTo(brandTextfield)
        }
        //modelTitle
        let modelTitle = TitleLabelWithRequired(R.string.localizable.model())
        backView.addSubview(modelTitle)
        modelTitle.snp.makeConstraints { make in
            make.top.equalTo(seriesTextfield.snp.bottom).offset(Constant.TopBottomMargin)
            make.left.equalTo(brandTitle)
            make.width.height.greaterThanOrEqualTo(0)
        }
        //modelTextfield
        backView.addSubview(modelTextfield)
        modelTextfield.snp.makeConstraints { make in
            make.top.equalTo(modelTitle.snp.bottom).offset(Constant.TitleTextMargin)
            make.left.right.equalTo(brandTextfield)
            make.height.greaterThanOrEqualTo(0)
        }
        //countTitle
        let countTitle = TitleLabelWithRequired(R.string.localizable.qty())
        backView.addSubview(countTitle)
        countTitle.snp.makeConstraints { make in
            make.top.equalTo(modelTextfield.snp.bottom).offset(Constant.TopBottomMargin)
            make.left.equalTo(brandTitle)
            make.width.height.greaterThanOrEqualTo(0)
        }
        //countTextfield
        backView.addSubview(countTextfield)
        countTextfield.snp.makeConstraints { make in
            make.top.equalTo(countTitle.snp.bottom).offset(Constant.TitleTextMargin)
            make.left.equalTo(brandTextfield)
            make.width.greaterThanOrEqualTo(0)
            make.height.equalTo(55)//greaterThanOrEqualTo(0),暂时处理this is requird不能正常显示的问题
        }
        //batteryRatingTextfield
        backView.addSubview(batteryRatingTextfield)
        batteryRatingTextfield.snp.makeConstraints { make in
            make.top.equalTo(countTextfield)
            make.left.equalTo(countTextfield.snp.right).offset(Constant.InternalMargin)
            make.right.equalTo(brandTextfield)
            make.width.equalTo(countTextfield)
            make.height.equalTo(DIYTextField.height)
        }
        //battery rating Title
        let batteryRatingTitle = ContentLabel(text: R.string.localizable.batteryRatingKWh())
        backView.addSubview(batteryRatingTitle)
        batteryRatingTitle.snp.makeConstraints { make in
            make.top.equalTo(countTitle)
            make.left.equalTo(batteryRatingTextfield)
            make.width.height.greaterThanOrEqualTo(0)
        }
        //partOfAggreTitle
        let partOfAggreTitle = ContentLabel()
        partOfAggreTitle.text = R.string.localizable.isTheBatterySystemPartOfAnAggregatedControl()
        partOfAggreTitle.numberOfLines = 0
        backView.addSubview(partOfAggreTitle)
        partOfAggreTitle.snp.makeConstraints { make in
            make.top.equalTo(countTextfield.snp.bottom).offset(15)
            make.left.equalTo(countTextfield)
            make.height.width.greaterThanOrEqualTo(0)
        }
        
        //partOfAggrefield
        partOfAggreSwitchBtn = SwitchButton()
        partOfAggreSwitchBtn.setContentHuggingPriority(.required, for: NSLayoutConstraint.Axis.horizontal)
        partOfAggreSwitchBtn.setContentCompressionResistancePriority(.required, for: NSLayoutConstraint.Axis.horizontal)
        backView.addSubview(partOfAggreSwitchBtn)
        partOfAggreSwitchBtn.snp.makeConstraints { make in
            make.left.equalTo(partOfAggreTitle.snp.right).offset(5)
            make.right.equalTo(-Constant.LeftRightMargin)
            make.size.equalTo(SwitchButton.size)
            make.centerY.equalTo(partOfAggreTitle)
        }
        //changedTitle
        let changedTitle = ContentLabel()
        changedTitle.text = R.string.localizable.hasTheInstallerChangedDefaultManufacturerSettingOfTheBatteryStorageSystem()
        changedTitle.numberOfLines = 0
        backView.addSubview(changedTitle)
        changedTitle.snp.makeConstraints { make in
            make.top.equalTo(partOfAggreSwitchBtn.snp.bottom).offset(25)
            make.left.equalTo(countTextfield)
            make.height.width.greaterThanOrEqualTo(0)
            make.bottom.equalTo(-20)
        }
        
        //changedTextfield
        changedSwitchBtn = SwitchButton()
        changedSwitchBtn.setContentHuggingPriority(.required, for: NSLayoutConstraint.Axis.horizontal)
        changedSwitchBtn.setContentCompressionResistancePriority(.required, for: NSLayoutConstraint.Axis.horizontal)
        backView.addSubview(changedSwitchBtn)
        changedSwitchBtn.snp.makeConstraints { make in
            make.left.equalTo(changedTitle.snp.right).offset(5)
            make.right.equalTo(partOfAggreSwitchBtn)
            make.size.equalTo(SwitchButton.size)
            make.centerY.equalTo(changedTitle)
        }
        changedSwitchBtn.addTarget(self, action: #selector(changedSwitchBtnClick(_:)), for: UIControl.Event.touchUpInside)
        partOfAggreSwitchBtn.addTarget(self, action: #selector(partOfAggreSwitchBtnClick(_:)), for: UIControl.Event.touchUpInside)
        deleteBtn.addTarget(self, action: #selector(deleteBtnClick), for: UIControl.Event.touchUpInside)
        //点击选择model
        self.modelTextfield.rx.tapGesture().when(.recognized).subscribe(onNext: {[weak self] _ in
            guard let strongSelf = self else {return}
            strongSelf.selectModelClosure?()
        }).disposed(by: self.disposeBag)
    }
    @objc func deleteBtnClick(){
        self.endEditing(true)
        self.deleteRowClosure?()
    }
    @objc func changedSwitchBtnClick(_ sender: SwitchButton){
        self.endEditing(true)
        self.changedSwitchClosure?(sender.tag == 1 ? true: false)
    }
    @objc func partOfAggreSwitchBtnClick(_ sender: SwitchButton){
        self.endEditing(true)
        self.partOfAggreSwitchClosure?(sender.tag == 1 ? true: false)
    }
    func setData(_ list: [InstallationDetailSystemDetailForBatteryModel],row: Int){
        guard list.count > row else {
            return
        }
        let model  = list[row]
        self.brandTextfield.text = model.battery_brand
        self.seriesTextfield.text = model.battery_series
        self.modelTextfield.textField.text = model.battery_model
        self.countTextfield.textField.text = (model.number_batteries == 0 ? "" : "\(model.number_batteries)")
        self.batteryRatingTextfield.text = model.battery_rating
        let partOfAggresFlag = model.battery_system_part_of_an_aggregated_control
        let changeBatteryFlag = model.changed_the_default_battery_manufacturer_setting
        self.partOfAggreSwitchBtn.switchOn(partOfAggresFlag)
        self.changedSwitchBtn.switchOn(changeBatteryFlag)
    }
}
